package com.etheart.datamanagement.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etheart.framework.core.dao.QueryService;
import com.etheart.framework.core.util.DateUtils;
import com.etheart.model.entity.Datamanagement;

@Service("datamanagement_DatamanagementService")
public class DatamanagementServiceImp implements InitializingBean, DatamanagementService { 
	private static Map<String, String> map = new HashMap<String, String>();

	/* (non-Javadoc)
	 * @see com.etheart.datamanagement.service.Datamanagement#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		if (map.isEmpty()) {
			List<Datamanagement> datamanagements = findAll();
			for (Datamanagement datamanagement : datamanagements) {
				map.put(datamanagement.getKey(), datamanagement.getValue());
			}
		}
	}
	@Autowired
	private QueryService queryService;

	private List<Datamanagement> findAll(){
		return queryService.findAll(Datamanagement.class);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.datamanagement.service.Datamanagement#getIntValue(java.lang.String)
	 */
	public Integer getIntValue(String key){
		String value = map.get(key);
		return Integer.parseInt(value);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.datamanagement.service.Datamanagement#getStringValue(java.lang.String)
	 */
	public String getStringValue(String key){
		return  map.get(key);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.datamanagement.service.Datamanagement#getDateValue(java.lang.String)
	 */
	public Date getDateValue(String key){
		String value = map.get(key);
		return DateUtils.parseDate(value); 
	}
	/* (non-Javadoc)
	 * @see com.etheart.datamanagement.service.Datamanagement#getTimeValue(java.lang.String)
	 */
	public Date getTimeValue(String key){
		String value = map.get(key);
		return DateUtils.parseTime(value); 
	}
}
